package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.FastParam;
import com.fastadmin.pojo.Role;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.builder.SQL;
import com.xphsc.easyjdbc.page.PageInfo;
import org.asynchttpclient.util.StringBuilderPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public class RoleMapper {

    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;


    public List<Role> getRolesByUserId(Long userId) {
        return null;
    }

    public List<Role> findByIds(Iterable roleIds) {
        String ids = CollUtil.join(roleIds, ",");
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	* ");
        sql.append(" FROM ");
        sql.append(" 	t_role ");
        sql.append(" WHERE ");
        sql.append(" 	id IN (?) ");
        return this.easyJdbcTemplate.find(sql.toString(), Role.class, ids);
    }

    public List<Role> findAll() {
        return easyJdbcTemplate.findAll(Role.class);
    }

    public PageInfo<Role> findByFastParam(FastParam param) {
        String sql = param.getSql("t_role");
        PageInfo<Role> pageInfo = this.easyJdbcTemplate.findByPage(sql, Role.class, param.offset, param.limit);
        return pageInfo;
    }

    public int deleteByIds(String ids) {
        List<String> split = StrUtil.split(ids, ',');
        int i = this.easyJdbcTemplate.deleteByIds(Role.class, split);
        return i;
    }

    public int update(Role role) {
        int update = this.easyJdbcTemplate.update(role);
        return update;
    }

    public Object save(Role role) {
        return this.easyJdbcTemplate.insertKey(role);
    }

    public Role findById(String ids) {
        return this.easyJdbcTemplate.getByPrimaryKey(Role.class, ids);

    }

    public List<Role> findByRole_key(String authorities) {
        List<Role> list = this.easyJdbcTemplate.find(
                SQL.BUILD()
                        .SELECT("*")
                        .FROM("t_role")
                        .WHERE("role_key=?"), Role.class, authorities

        );
        return list;
    }

}
